function out = waldTestRegimeShift_spec(theta2,AVarTheta2,nx)

% Test for change in intercepts if possible
if any(AVarTheta2.switch_h0 == 1)
    numRes    = sum(AVarTheta2.switch_h0);
    numTheta2 = length(AVarTheta2.names);
    pos_h0_1  = zeros(numTheta2,numRes);
    pos_h0_2  = zeros(numTheta2,numRes);
    idx       = 0;
    for i=1:nx
        if AVarTheta2.switch_h0(i,1) == 1
            idx = idx + 1;
            pos_h0_1(:,idx) = strcmp(['h0',num2str(i),'_1'],AVarTheta2.names);
            pos_h0_2(:,idx) = strcmp(['h0',num2str(i),'_2'],AVarTheta2.names);
        end
    end
    R = (pos_h0_1-pos_h0_2)';
    if numTheta2 ~= length(fieldnames(theta2))
        theta2Test = reduceTheta2(theta2,AVarTheta2.names);
    else
        theta2Test = theta2;
    end
    theta2TestValues = struct2array(theta2Test)';
    Q_intercept = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
    pValue_intercept = chi2cdf(Q_intercept,numRes,'upper');
else
    Q_intercept = NaN;
    pValue_intercept = NaN;
end

% Test for change in slopes
if any(any(AVarTheta2.switch_hx == 1))
    numRes    = sum(sum(AVarTheta2.switch_hx));
    numTheta2 = length(AVarTheta2.names);
    pos_hx_1 = zeros(numTheta2,numRes);
    pos_hx_2 = zeros(numTheta2,numRes);
    idx      = 0;
    for i=1:nx
        for j=1:nx
            if AVarTheta2.switch_hx(i,j) == 1
                idx = idx + 1;
                pos_hx_1(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
                pos_hx_2(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
            end
        end
    end
    R = (pos_hx_1-pos_hx_2)';
    if numTheta2 ~= length(fieldnames(theta2))
        theta2Test = reduceTheta2(theta2,AVarTheta2.names);
    else
        theta2Test = theta2;
    end
    theta2TestValues = struct2array(theta2Test)';
    Q_slope = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
    pValue_slope = chi2cdf(Q_slope,numRes,'upper');
else
    Q_slope = NaN;
    pValue_slope = NaN;
end

% Joint test for change in intercepts and slope if possible
if any(AVarTheta2.switch_h0 == 1) && any(any(AVarTheta2.switch_hx == 1))
    numRes_h0 = sum(AVarTheta2.switch_h0);
    numTheta2 = length(AVarTheta2.names);
    pos_h0_1  = zeros(numTheta2,numRes_h0);
    pos_h0_2  = zeros(numTheta2,numRes_h0);
    idx       = 0;
    for i=1:nx
        if AVarTheta2.switch_h0(i,1) == 1
            idx = idx + 1;
            pos_h0_1(:,idx) = strcmp(['h0',num2str(i),'_1'],AVarTheta2.names);
            pos_h0_2(:,idx) = strcmp(['h0',num2str(i),'_2'],AVarTheta2.names);
        end
    end    
    numRes_hx  = sum(sum(AVarTheta2.switch_hx));
    pos_hx_1 = zeros(numTheta2,numRes_hx);
    pos_hx_2 = zeros(numTheta2,numRes_hx);
    idx      = 0;
    for i=1:nx
        for j=1:nx
            if AVarTheta2.switch_hx(i,j) == 1
                idx = idx + 1;
                pos_hx_1(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_1'],AVarTheta2.names);
                pos_hx_2(:,idx) = strcmp(['hx',num2str(i),num2str(j),'_2'],AVarTheta2.names);
            end
        end
    end
    R = [(pos_h0_1-pos_h0_2)';(pos_hx_1-pos_hx_2)'];
    if numTheta2 ~= length(fieldnames(theta2))
        theta2Test = reduceTheta2(theta2,AVarTheta2.names);
    else
        theta2Test = theta2;
    end
    theta2TestValues = struct2array(theta2Test)';
    Q_InterceptSlope = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
    pValue_InterceptSlope = chi2cdf(Q_InterceptSlope,numRes_h0+numRes_hx,'upper');
else
    Q_InterceptSlope = NaN;
    pValue_InterceptSlope = NaN;
end

% Test for whether gamax(i,1) == gamax(i+1,1) for i=1:nx-1
numTheta2 = length(AVarTheta2.names);
pos_gamax = zeros(numTheta2,nx);
for i=1:nx
    pos_gamax(:,i) = strcmp(['gamax',num2str(i)],AVarTheta2.names);
end
R = zeros(nx-1,numTheta2);
for i=1:nx-1
   R(i,:) =  [pos_gamax(:,1)-pos_gamax(:,i+1)]';
end
theta2TestValues = struct2array(theta2Test)';
Q_shorRate = (R*theta2TestValues)'*inv(R*AVarTheta2.VarRobust*R')*(R*theta2TestValues);
pValue_shorRate = chi2cdf(Q_shorRate,size(R,1),'upper');

% The output
out.Q_intercept      = Q_intercept;
out.pValue_intercept = pValue_intercept;
out.Q_slope          = Q_slope;
out.pValue_slope     = pValue_slope;
out.Q_InterceptSlope = Q_InterceptSlope;
out.pValue_InterceptSlope = pValue_InterceptSlope;
out.Q_shorRate       = Q_shorRate;
out.pValue_shorRate  = pValue_shorRate;